﻿using System;
using System.ComponentModel;

namespace Notoric.ViewModel.Elements.CWNotation
{
    /// <summary>
    /// Represents one ledger line.
    /// </summary>
    public class LedgerLine : VirtualViewModelElement
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="LedgerLine"/> instance.
        /// </summary>
        /// <param name="owner">
        /// <para>
        /// Type: <see cref="Chord"/>
        /// </para>
        /// <para>
        /// The chord that directly owns this line.
        /// </para>
        /// </param>
        /// <param name="x1">
        /// <para>
        /// Type: <see cref="Double"/>
        /// </para>
        /// <para>
        /// The x-coordinate of the line starting point.
        /// </para>
        /// </param>
        /// <param name="x2">
        /// <para>
        /// Type: <see cref="Double"/>
        /// </para>
        /// <para>
        /// The x-coordinate of the line ending point.
        /// </para>
        /// </param>
        /// <param name="y">
        /// <para>
        /// Type: <see cref="Double"/>
        /// </para>
        /// <para>
        /// The y-coordinate of the line.
        /// </para>
        /// </param>
        internal LedgerLine(Chord owner, double x1, double x2, double y)
            : base(owner)
        {
            this.x1 = x1;
            this.x2 = x2;
            this.y = y;

            RegisterToOwnerPropertyChanged<Chord>();
            RegisterToOwnerPropertyChanged<Staff>();
        }

        /// <summary>
        /// Creates a new <see cref="LedgerLine"/> instance.
        /// </summary>
        /// <param name="owner">
        /// <para>
        /// Type: <see cref="Chord"/>
        /// </para>
        /// <para>
        /// The chord that directly owns this line.
        /// </para>
        /// </param>
        internal LedgerLine(Chord owner)
            : base(owner)
        {
            RegisterToOwnerPropertyChanged<Chord>();
            RegisterToOwnerPropertyChanged<Staff>();
        }

        #endregion

        #region VirtualViewModelElement Members

        /// <inheritdoc/>
        public override void Dispose()
        {
            UnregisterFromOwnerPropertyChanged<Chord>();
            UnregisterFromOwnerPropertyChanged<Staff>();
            base.Dispose();
        }

        #endregion

        #region Event Handlers

        /// <inheritdoc/>
        protected override void Owner_PropertyChanged(ViewModelElement owner, PropertyChangedEventArgs e)
        {
            if (owner is Chord && e.PropertyName == "Position")
            {
                RaisePropertyChanged("X1");
                RaisePropertyChanged("X2");
            }
            else if (owner is Staff && e.PropertyName == "Top" || e.PropertyName == "Height" || e.PropertyName == "SpaceSize")
                RaisePropertyChanged("Y");
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the x-coordinate of the line start point.
        /// </summary>
        /// <value>
        /// <para>
        /// Type: <see cref="Double"/>
        /// </para>
        /// <para>
        /// <b>Caution: The value of this property is different when it is being set and when it is being read!</b>
        /// </para>
        /// <para>
        /// When this property is being set, its value equals the offset
        /// (in device independent pixels) of the line's starting point
        /// from the owner <see cref="Chord"/>'s <see cref="CoreSymbol.Position"/>.<br/>
        /// When this property is being read, its value equals the offset
        /// (in device independent pixels) of the line's starting point
        /// from the left edge of the section.
        /// </para>
        /// </value>
        public double X1
        {
            get { return x1 + FindOwner<Chord>().Position; }
            set
            {
                x1 = value;
                RaisePropertyChanged("X1");
            }
        }

        /// <summary>
        /// Gets or sets the x-coordinate of the line end point.
        /// </summary>
        /// <value>
        /// <para>
        /// Type: <see cref="Double"/>
        /// </para>
        /// <para>
        /// <b>Caution: The value of this property is different when it is being set and when it is being read!</b>
        /// </para>
        /// <para>
        /// When this property is being set, its value equals the offset
        /// (in device independent pixels) of the line's ending point
        /// from the owner <see cref="Chord"/>'s <see cref="CoreSymbol.Position"/>.<br/>
        /// When this property is being read, its value equals the offset
        /// (in device independent pixels) of the line's ending point
        /// from the left edge of the section.
        /// </para>
        /// </value>
        public double X2
        {
            get { return x2 + FindOwner<Chord>().Position; }
            set
            {
                x2 = value;
                RaisePropertyChanged("X2");
            }
        }

        /// <summary>
        /// Gets or sets the y-coordinate of the line.
        /// </summary>
        /// <value>
        /// <para>
        /// Type: <see cref="Double"/>
        /// </para>
        /// <para>
        /// <b>Caution: The value of this property is different when it is being set and when it is being read!</b>
        /// </para>
        /// <para>
        /// When this property is being set, its value equals the order of the ledger line:
        /// (-1 stands for the 1st ledger line bellow the staff, +1 for the 1st ledger line above the staff).<br/>
        /// When this property is being read, its value equals offset (in device independent pixels) 
        /// of the line from the top edge of the section.
        /// </para>
        /// </value>
        public double Y
        {
            get 
            {
                var staff = FindOwner<Staff>();
                var basePosition = (y > 0 ? staff.Top : staff.Bottom);

                return basePosition - y * staff.SpaceSize; 
            }
            set
            {
                y = value;
                RaisePropertyChanged("Y");
            }
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the value of the <see cref="X1"/> property.
        /// </summary>
        private double x1;
        /// <summary>
        /// Holds the value of the <see cref="X2"/> property.
        /// </summary>
        private double x2;
        /// <summary>
        /// Holds the value of the <see cref="Y"/> property.
        /// </summary>
        private double y;

        #endregion
    }
}
